export function showToast(msg, callback = function() {

}) {
	uni.showToast({
		title: msg,
		icon: 'none',
		duration: 2000,
		mask: true,
		success() {
			setTimeout(function() {
				callback()
			}, 2000)
		}
	})
}
export function getSalaryData(select, step = 1000, num = 80) {
	let start = [];
	let end = [];
	for (let i = 1; i <= num; i++) {
		let text = i * step;
		start.push(text);
		if (select && select + 5 >= i && i >= (select + 1)) {
			end.push(text)
		}
	}
	if (select == num) {
		end.push(select * step)
	}
	return [start, end];
}
export function getCustomNav() {
	let result = {};
	uni.getSystemInfo({
		success(e) {
			let myStatusBar = 0;
			let myCustomBar = 0;
			let myNvaMartom = 0;
			let myNavHeight = 0;
			let myNavWidth = 0;
			// #ifdef MP-WEIXIN
			let custom = uni.getMenuButtonBoundingClientRect();
			// #endif
			myStatusBar = e.statusBarHeight;
			myNavHeight = custom.height;
			myNavWidth = custom.width;
			myNvaMartom = custom.top - e.statusBarHeight;
			myCustomBar = (myNvaMartom * 2 + custom.height) - 2;
			result.statusBar = myStatusBar * 750 / wx.getSystemInfoSync().windowWidth; //顶部状态栏高度
			result.customBar = (myCustomBar * 750 / wx.getSystemInfoSync().windowWidth) + 12; //导航栏高度
			result.nvaMartom = myNvaMartom * 750 / wx.getSystemInfoSync().windowWidth; //胶囊上边距
			result.navHeight = myNavHeight * 750 / wx.getSystemInfoSync().windowWidth; //胶囊按钮高度
			result.navWidth = myNavWidth * 750 / wx.getSystemInfoSync().windowWidth; //胶囊按钮宽度
			result.navMarRight = 750 - (custom.right * 750 / wx.getSystemInfoSync().windowWidth); //胶囊右边距
			result.pageMagTop = result.customBar + result.statusBar; //页面内容距离顶部的上边距，导航栏全部高度
		}
	})
	return result
}
export function multipleChoiceChange(arr, value, max = -1) {
	if (!arr.includes(value)) {
		if (max == -1 || arr.length < max) {
			arr.push(value)
		} else {
			uni.showToast({
				title: `最多只能选择${max}个`,
				icon: 'none'
			})
		}
	} else {
		const index = arr.indexOf(value)
		arr.splice(index, 1)
	}
	return arr;
}
export function multipleChoiceChangeObj(targetName, value, vm, max = -1) {
	const data = vm[targetName]
	if (!data[value.id]) {
		if (max == -1 || Object.keys(data).length < max) {
			vm.$set(vm[targetName], value.id, value)
		} else {
			uni.showToast({
				title: `最多只能选择${max}个`,
				icon: 'none',
				mask: true,
			})
		}
	} else {
		vm.$delete(vm[targetName], value.id)
	}
}

export function getCurrentTime(fields = 's', time = null) {
	const date = time ? new Date(time) : new Date();
	let str = '';
	let y = date.getFullYear().toString();
	let m = (date.getMonth() + 1).toString();
	m = m.length == 1 ? '0' + m : m
	let d = date.getDate().toString();
	d = d.length == 1 ? '0' + d : d
	let h = date.getHours().toString();
	h = h.length == 1 ? '0' + h : h
	let i = date.getMinutes().toString();
	i = i.length == 1 ? '0' + i : i
	let s = date.getSeconds().toString();
	s = s.length == 1 ? '0' + s : s
	if (fields == 'y') {
		str = y;
	}
	if (fields == 'm') {
		str = y + '-' + m
	}
	if (fields == 'd') {
		str = y + '-' + m + '-' + d
	}
	if (fields == 'h') {
		str = y + '-' + m + '-' + d + ' ' + h
	}
	if (fields == 'i') {
		str = y + '-' + m + '-' + d + ' ' + h + ':' + i
	}
	if (fields == 's') {
		str = y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s
	}
	return str;
}
export function get24HoursTime(time) {
	if (time.indexOf('上午') != -1) {
		time = time.replace('上午', '');
		let arr = time.split(':');
		arr[0] = arr[0] == '12' ? '00' : arr[0];
		arr[0] = arr[0].length == 2 ? arr[0] : '0' + arr[0];
		time = arr.join(':')
	}
	if (time.indexOf('下午') != -1) {
		time = time.replace('下午', '');
		let arr = time.split(':');
		arr[0] = arr[0] == '12' ? arr[0] : arr[0] * 1 + 12;
		arr[0] = arr[0] == '24' ? '00' : arr[0].toString()
		arr[0] = arr[0].length == 2 ? arr[0] : '0' + arr[0];
		time = arr.join(':')
	}
	return time;
}
export function getAuthorize(scopeName, authorizeName) {
	return new Promise((resolve, reject) => {
		uni.authorize({
			scope: scopeName,
			success: () => {
				resolve()
			},
			fail: () => {
				uni.showModal({
					title: '提示',
					content: '您拒绝了' + authorizeName + '授权，请前往设置页开启',
					confirmText: '去开启',
					success(e) {
						if (e.confirm) {
							uni.openSetting()
						}
					}
				})
				reject()
			},
		})
	})

}
export function getLocation() {
	return new Promise((resolve, reject) => {
		uni.getLocation({
			isHighAccuracy: true,
			geocode: true,
			success(e) {
				resolve(e)
			},
			fail(e) {
				reject()
			}
		})
	})
}
export function chooseLocation(latitude, longitude) {
	return new Promise((resolve, reject) => {
		uni.chooseLocation({
			latitude,
			longitude,
			success(e) {
				resolve({
					address: e.address,
					latitude: e.latitude,
					longitude: e.longitude,
					name: e.name
				})
			},
			fail() {
				uni.showToast({
					title: '位置选择失败',
					icon: 'none',
					mask: true
				})
				reject()
			}
		})
	})
}
export function isMore(page, limit, total) {
	return page * limit < total;
}
export function keyValueToArray(data) {
	let res = [];
	Object.keys(data).forEach(item => {
		res.push({
			id: item,
			name: data[item]
		})
	})
	return res;
}

export function isObjEqual(o1, o2, ignore = []) {
	o1 = JSON.parse(JSON.stringify(o1));
	o2 = JSON.parse(JSON.stringify(o2));
	ignore.forEach(item => {
		delete o1[item]
		delete o2[item]
	})
	var props1 = Object.keys(o1);
	var props2 = Object.keys(o2);
	if (props1.length != props2.length) {
		return false;
	}
	for (var i = 0, max = props1.length; i < max; i++) {
		var propName = props1[i];
		if (ignore.includes(propName)) continue;
		if (o1[propName] !== o2[propName]) {
			return false;
		}
	}
	return true;
}